<template>
	<view class="almost-lottery">
		<view style="height: 300rpx;">
			<image @click="toPageClick" src="/static/toPage.png" mode="widthFix" class="toPage"></image>
		</view>
		<view class="almost-lottery__wheel">
			<almost-lottery ref="almostRef" :lottery-size="lotteryConfig.lotterySize"
				:action-size="lotteryConfig.actionSize" :ring-count="2" :duration="1" :self-rotaty="false"
				:img-circled="true" :canvasCached="true" :prize-list="prizeList" :prize-index="prizeIndex"
				@reset-index="prizeIndex = -1" @draw-before="handleDrawBefore" @draw-start="handleDrawStart"
				@draw-end="handleDrawEnd" @finish="handleDrawFinish" v-if="prizeList.length" />
			<view class="almost-lottery__count">
				<text class="text">当前剩余次数： {{ freeNumDay }} / 6</text>
			</view>
			<view class="bottonCenter">
				<u-button :disabled="disabled" :custom-style="customStyleBtn1" @click="submit" :loading="loading">
					{{isItAdFree?'去抽奖':'看广告转一次'}}
				</u-button>
				<view class="jlStyle" @click="setInfo">中将记录</view>
			</view>
		</view>
		<lotteryInfoVue ref="infoRef" :show="lotteryInfoShow" @success="handleInfo"/>
	</view>
</template>

<script>
	import lotteryInfoVue from './components/lotteryInfo.vue'
	import {
		myUser
	} from '@/api/my.js'
	import AlmostLottery from '@/uni_modules/almost-lottery/components/almost-lottery/almost-lottery.vue'
	import {
		clearCacheFile,
		clearStore
	} from '@/uni_modules/almost-lottery/utils/almost-utils.js'
	import {
		lotteryDrawList,
		lottery,
		lotteryCount,
		userAdvertising
	} from '@/api/index.js'
	export default {
		name: 'Home',
		components: {
			AlmostLottery,
			lotteryInfoVue
		},
		data() {
			return {
				lotteryInfoShow:false,
				customStyleBtn1: {
					width: '616rpx',
					height: '96rpx',
					borderRadius: '96rpx',
					background: 'linear-gradient(to right, #0492FF, #0AE5FD)',
					fontSize: '35rpx',
					fontWeight: 800,
					color: '#FFFFFF',
					border: 'none',
					lineHight: '50rpx !important',
					letterSpacing: '1px',
					isEye: false
				},
				// 开启调试模式
				isDev: true,
				disabled: false,
				loading: false,
				// 以下是转盘配置相关数据
				lotteryConfig: {
					// 抽奖转盘的整体尺寸，单位rpx
					lotterySize: 600,
					// 抽奖按钮的尺寸，单位rpx
					actionSize: 200
				},

				// 以下是转盘 UI 配置
				// 转盘外环图，如有需要，请参考替换为自己的设计稿
				lotteryBg: require('@/static/lottery-bg.png'),
				// 抽奖按钮图
				actionBg: require('@/static/action-bg.png'),

				// 以下是奖品配置数据
				// 奖品数据
				prizeList: [],
				// 奖品是否设有库存
				onStock: true,
				// 中奖下标
				prizeIndex: -1,

				// 是否正在抽奖中，避免重复触发
				prizeing: false,

				// 以下为中奖概率有关数据
				// 是否由前端控制概率，默认不开启，强烈建议由后端控制
				onFrontend: false,
				// 权重随机数的最大值
				prizeWeightMax: 0,
				// 权重数组
				prizeWeightArr: [],

				// 以下为业务需求有关示例数据
				// 金币余额
				goldCoin: 20,
				// 当日免费抽奖次数余额
				freeNum: 6,
				// 每次消耗的金币数
				goldNum: 20,
				// 每天免费抽奖次数
				freeNumDay: null,
				user: {},
				flag: false,
				prizeId: 1,
				isItAdFree:false
			}
		},
		computed: {
			isApple() {
				return uni.getSystemInfoSync().platform === 'ios'
			}
		},
		onShow() {
			this.getlotteryCount()
			this.getUserAdvertising()
		},
		onLoad() {
			
			this.getUser()
			callback(true)
		},
		methods: {
			setInfo(){
				this.lotteryInfoShow = true
				this.$refs.infoRef.getList()
			},
			handleInfo(){
				this.lotteryInfoShow = false
			},
			toPageClick(){
				uni.navigateBack()
			},
			// 是否免广告
			async getUserAdvertising(){
				let res = await userAdvertising()
				this.isItAdFree = res.data
				if(this.isItAdFree){
					setTimeout(()=>{
						if(this.freeNumDay !=0 && this.isItAdFree){
							this.disabled = false
						}
					},500)
					
				}
			},
			async getlotteryCount(){
				let res = await lotteryCount()
				this.freeNumDay = res.data
				if(res.data==0){
					this.disabled = true
				}
			},
			// 看广告一次
			 submit() {
				 this.loading = true
				 if(this.isItAdFree){
					 // this.loading = false
					 this.flag = true
					 lottery().then(res=>{
					 	this.prizeId = res.data.number
					 	setTimeout(()=>{
					 		this.$refs.almostRef.handleActionStart()
							this.loading = false
					 	},1500)
					 	this.getlotteryCount()
					 })
					 return
				 }
				// this.loading = true
				window.android.showRewardAd(this.user.userid, this.user.idNumber)
				setTimeout(()=>{
					this.loading = false
				},1500)
				//激励成功观看成功
				window.onRewardSuccess =  () => {
					// this.loading = false
					this.flag = true
					lottery().then(res=>{
						this.prizeId = res.data.number-1
						setTimeout(()=>{
							this.$refs.almostRef.handleActionStart()
						},1500)
						this.getlotteryCount()
					})
				}
				//激励失败观看失败
				window.onRewardAdLoadError = (code, msg) => {
					// this.loading = false
				}
			},
			async getUser() {
				let res = await myUser()
				this.user = res.data

			},
			// 重新生成
			handleInitCanvas() {
				clearCacheFile()
				clearStore()
				this.prizeList = []
				this.getPrizeList()
			},
			// 通过 popup 打开
			handleCheckPopup() {
				uni.navigateTo({
					url: '/pages/popup/popup'
				})
			},
			setImg(lotteryType) {
				if (lotteryType == 1) { //能量
					return require('@/static/public-images/points.png')
				}
				if (lotteryType == 2) { //石头  
					return require('@/static/stone-images/0.png')

				}
				if (lotteryType == 3) { //宝箱
					return require('@/static/public-images/bx.png')
				}
				if (lotteryType == 4) { //积分
					return require('@/static/public-images/point.png')
				}

			},
			// 获取奖品列表
			async getPrizeList() {
				uni.showLoading({
					title: '奖品准备中...'
				})

				let res1 = await lotteryDrawList()
				let arr = res1.data.map((item, index) => {
					return {
						prizeId: index,
						prizeName: item.lotteryName,
						prizeStock: 10000,
						prizeWeight: 200,
						prizeImage: this.setImg(item.lotteryType)
					}
				})
				let datas = {
					ok: true,
					data: arr
				}
				let res = datas
				console.log('获取奖品列表', res)

				if (res.ok) {
					let data = res.data
					if (data.length) {
						this.prizeList = data

						// 计算开始绘制的时间
						if (console.time) {

						}

						// 如果开启了前端控制概率
						// 得出权重的最大值并生成权重数组
						if (this.onFrontend) {
							// 生成权重数组并排序取得最大值
							this.prizeWeightArr = this.prizeList.map(item => item.prizeWeight)
							let prizeWeightArrSort = [...this.prizeWeightArr]
							prizeWeightArrSort.sort((a, b) => b - a)

							// 开放自定义权重最大值，没有自定义则取权重数组中的最大值
							this.prizeWeightMax = this.prizeWeightMax > 0 ? this.prizeWeightMax : prizeWeightArrSort[0]
						}
					}
				} else {
					uni.hideLoading()
					uni.showToast({
						title: '获取奖品失败',
						mask: true,
						icon: 'none'
					})
				}
			},
			// 模拟请求 获取奖品列表 接口，
			// 注意这里返回的是一个 Promise
			// 大哥，这里只是模拟，别告诉我你不会对接自己的接口
			requestApiGetPrizeList() {

				return new Promise((resolve, reject) => {

					let requestTimer = setTimeout(() => {
						clearTimeout(requestTimer)
						requestTimer = null

						// prizeStock 奖品库存
						// prizeWeight 中奖概率，数值越大中奖概率越高，权重一样时随机中奖
						resolve({
							ok: true,
							data: [{
									prizeId: 1,
									prizeName: '0.1元现金',
									prizeStock: 10,
									prizeWeight: 200,
									prizeImage: require('@/static/git.png')
								},
								{
									prizeId: 2,
									prizeName: '10元现金',
									prizeStock: 0,
									prizeWeight: 50,
									prizeImage: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/56f085e0-bcfe-11ea-b244-a9f5e5565f30.png'
								},
								{
									prizeId: 3,
									prizeName: '5元话费',
									prizeStock: 1,
									prizeWeight: 80
								},
								{
									prizeId: 4,
									prizeName: '50元现金',
									prizeStock: 0,
									prizeWeight: 10,
									prizeImage: ''
								},
								{
									prizeId: 5,
									prizeName: '1卷抽纸',
									prizeStock: 3,
									prizeWeight: 3000,
									prizeImage: ''
								},
								{
									prizeId: 6,
									prizeName: '0.2元现金',
									prizeStock: 8,
									prizeWeight: 120
								},
								{
									prizeId: 7,
									prizeName: '谢谢参与',
									prizeStock: 100,
									prizeWeight: 10000
								},
								{
									prizeId: 8,
									prizeName: '100金币',
									prizeStock: 100,
									prizeWeight: 3000
								}
							]
						})
					}, 200)
				})
			},
			// 抽奖开始之前
			handleDrawBefore(callback) {
				// 调看广告
				// this.submit()

				let flag = this.flag

				callback(flag)
			},
			// 本次抽奖开始
			handleDrawStart() {
				console.log('触发抽奖按钮')
				if (this.prizeing) return
				this.prizeing = true

				this.tryLotteryDraw()
			},
			// 尝试发起抽奖
			tryLotteryDraw() {
				console.log('旋转开始，获取中奖下标......')
				// 判断是否由前端控制概率
				if (this.onFrontend) {
					this.localGetPrizeIndex()
				} else {
					this.remoteGetPrizeIndex()
				}
			},
			// 本地获取中奖下标
			localGetPrizeIndex() {
				// 前端控制概率的情况下，需要拿到最接近随机权重且大于随机权重的值
				// 后端控制概率的情况下，通常会直接返回 prizeId
				if (!this.prizeWeightMax || !this.prizeWeightArr.length) {
					return
				}

				// 注意这里使用了 Math.ceil，如果某个权重的值为 0，则始终无法中奖
				let randomWeight = Math.ceil(Math.random() * this.prizeWeightMax)

				// 生成大于等于随机权重的数组
				let tempMaxArrs = []
				this.prizeList.forEach((item) => {
					if (item.prizeWeight >= randomWeight) {
						tempMaxArrs.push(item.prizeWeight)
					}
				})

				// 如果大于随机权重的数组有值，先对这个数组排序然后取值
				// 反之新建一个临时的包含所有权重的已排序数组，然后取值
				let tempMaxArrsLen = tempMaxArrs.length
				if (tempMaxArrsLen) {
					tempMaxArrs.sort((a, b) => a - b)
					// 取值时，如果存在多个值，分两种情况
					if (tempMaxArrsLen > 1) {
						// 检查是否存在重复的值
						let sameCount = 0
						for (let i = 0; i < tempMaxArrs.length; i++) {
							if (tempMaxArrs[i] === tempMaxArrs[0]) {
								sameCount++
							}
						}

						// 值不相等的情况下取最接近的值，也就是第1个值
						if (sameCount === 1) {
							this.prizeIndex = this.prizeWeightArr.indexOf(tempMaxArrs[0])
						} else {
							// 存在值相等时，随机取值，当然这里你可以自己决定是否随机取值
							let sameWeight = tempMaxArrs[0]
							let sameWeightArr = []
							let sameWeightItem = {}
							this.prizeWeightArr.forEach((item, index) => {
								if (item === sameWeight) {
									sameWeightArr.push({
										prizeWeight: item,
										index
									})
								}
							})
							console.log('sameWeightArr', sameWeightArr)
							sameWeightItem = sameWeightArr[Math.floor(Math.random() * sameWeightArr.length)]
							console.log('sameWeightItem', sameWeightItem)
							this.prizeIndex = sameWeightItem.index
						}
					} else {
						this.prizeIndex = this.prizeWeightArr.indexOf(tempMaxArrs[0])
					}
				}

				// 如果奖品设有库存
				if (this.onStock) {

				}
			},
			// 远程请求接口获取中奖下标
			// 大哥，这里只是模拟，别告诉我你不会对接自己的接口
			remoteGetPrizeIndex() {
				console.warn('###当前处于模拟的请求接口，并返回了中奖信息###')
				// 模拟请求接口获取中奖信息
				let stoTimer = setTimeout(() => {
					stoTimer = null
					let list = [...this.prizeList]
					// 这里随机产生的 prizeId 是模拟后端返回的 prizeId
					// let prizeId = Math.floor(Math.random() * list.length + 1)
					let prizeId = this.prizeId
					// 拿到后端返回的 prizeId 后，开始循环比对得出那个中奖的数据
					for (let i = 0; i < list.length; i++) {
						let item = list[i]
						if (item.prizeId === prizeId) {
							// 中奖下标
							this.prizeIndex = i
							break
						}
					}

					// 如果奖品设有库存
					if (this.onStock) {

					}
				}, 200)
			},
			// 本次抽奖结束
			handleDrawEnd() {
				// 旋转结束后，开始处理拿到结果后的逻辑
				let prizeName = this.prizeList[this.prizeIndex].prizeName
				let tipContent = ''

				if (prizeName === '谢谢参与') {
					tipContent = '很遗憾，没有中奖，请再接再厉！'
				} else {
					// 如果奖品设有库存
					if (this.onStock) {
						let prizeStock = this.prizeList[this.prizeIndex].prizeStock
						tipContent = prizeStock ? `恭喜您，获得 ${prizeName}` : `很抱歉，您来晚了，当前奖品 ${prizeName} 已无库存`
					} else {
						tipContent = `恭喜您，获得 ${prizeName} ！`
					}
				}
				this.flag = false
				uni.showModal({
					content: tipContent,
					showCancel: false,
					complete: () => {
						this.prizeing = false
					}
				})
			},
			// 抽奖转盘绘制完成
			handleDrawFinish(res) {
				if (res.ok) {
					// 计算结束绘制的时间
					if (console.timeEnd) {

					}
				}

				let stoTimer = setTimeout(() => {
					stoTimer = null
					uni.hideLoading()
				}, 50)
			}
		},
		onLoad() {
			this.prizeList = []
			this.getPrizeList()
		},
		onUnload() {
			uni.hideLoading()
		}
	}
</script>

<style lang="scss" scoped>
	.jlStyle{
		margin-top: 30rpx;
		color: #FAFEDB;
	}
	.toPage{
		width: 50rpx;
		margin-top: 80rpx;
		margin-left: 30rpx;
	}
	.bottonCenter {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.sunmit {
		width: 350rpx;
		height: 90rpx;
		background-color: #F9FC31;
		border-radius: 15rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	page {
		width: 100%;
		height: 100vh;
		background-color: #FF893F;
		background: linear-gradient(to bottom, #FE7007, #FE6700);
	}

	.almost-lottery {
		// flex: 1;
		// background-color: #FF893F;
		// background: linear-gradient(to bottom, #FE7007, #FE6700);
	}

	.almost-lottery__head {
		position: relative;
		width: 100%;
		height: 640rpx;
		background: url('~static/images/lottery/top-bg.png') no-repeat center center/cover;

		.action {
			position: relative;
			top: 400rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			width: 480rpx;
			height: 88rpx;
			line-height: 88rpx;
			margin: 0 auto;
			color: #FFFFFF;
			font-size: 32rpx;
			background-color: rgba(255, 136, 61, 1);
			border-radius: 44rpx;
		}

		.action-shadow {
			box-shadow: 0px 14rpx 0px 0px rgba(235, 112, 36, 1);
		}

		.gold {
			width: 44rpx;
			height: 44rpx;
			margin-right: 10rpx;
			background-repeat: no-repeat;
			background-position: center center;
			background-size: contain;
			background-image: url("~static/images/lottery/gold.png");

			@media (-webkit-min-device-pixel-ratio: 2),
			(min-device-pixel-ratio: 2) {
				background-image: url("~static/images/lottery/gold@2x.png");
			}

			@media (-webkit-min-device-pixel-ratio: 3),
			(min-device-pixel-ratio: 3) {
				background-image: url("~static/images/lottery/gold@3x.png");
			}
		}

		.num {
			color: #F9FC31;
		}

		.tip {
			position: relative;
			top: 428rpx;
			color: #FFFFFF;
			font-size: 24rpx;
			text-align: center;
		}
	}

	.almost-lottery__wheel {
		text-align: center;

		.almost-lottery__count {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			text-align: center;
			padding: 40rpx 0;
		}

		.text {
			color: #FFFFFF;
			font-size: 35rpx;
		}
	}

	.almost-lottery__rule {
		padding: 0 28rpx;
		color: #FFF8CB;

		.rule-head {
			display: flex;
			justify-content: space-around;
			align-items: center;
			margin: 40rpx 0;

			.line {
				flex: 1;
				height: 1px;
				background-color: #FFF3A5;
			}

			.title {
				width: 280rpx;
				color: #F63857;
				line-height: 70rpx;
				text-align: center;
				margin: 0 20rpx;
				border-radius: 8rpx;
				background-image: linear-gradient(0deg, rgba(255, 242, 158, 1), rgba(255, 244, 168, 1));
			}
		}

		.rule-body {
			color: #FFF8CB;
			font-size: 24rpx;
			padding: 10rpx 0 40rpx;

			.item {
				display: flex;
				margin-bottom: 10rpx;
			}

			.number {
				position: relative;
				top: 4rpx;
				width: 28rpx;
				height: 28rpx;
				line-height: 28rpx;
				text-align: center;
				color: #F63857;
				background: #FFF8CB;
				border-radius: 50%;
				margin-right: 10rpx;
			}

			.text {
				flex: 1;
			}

			.item-rule .text {
				display: flex;
				flex-direction: column;
			}
		}
	}

	.almost-lottery__action-dev {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		width: 400rpx;
		height: 80rpx;
		border-radius: 10rpx;
		text-align: center;
		background-color: red;
		margin: 0 auto 40rpx;

		.text {
			color: #FFFFFF;
			font-size: 34rpx;
		}
	}

	.almost-lottery__popup-wrap {
		flex: 1;
		display: flex;
		justify-content: center;
		align-items: center;

		.almost-lottery {
			background: transparent;
		}
	}
</style>